﻿using System.Collections.Generic;
using System.Drawing;
using System.Runtime.Versioning;

#pragma warning disable IDE0130 // 命名空间与文件夹结构不匹配
namespace zykControl
#pragma warning restore IDE0130 // 命名空间与文件夹结构不匹配
{

    /// <summary>
    /// 表示zykHFGrid控件的列集合
    /// </summary>
    /// <remarks></remarks>
    [SupportedOSPlatform("windows")]
    public class ZykHFGridColumns(ZykHFGrid theHFGrid)
    {
        private readonly ZykHFGrid mHFGrid = theHFGrid;
        private readonly List<ZykHFGridColumn> mItems = [];

        public int Count
        {
            get
            {
                return mItems.Count;
            }
        }

        private int mWidth = 0;

        private int GetWidthTotal()
        {
            int dWidth = 0;
            for (int xx = 0, loopTo = Count - 1; xx <= loopTo; xx++)
            {
                if (this[xx].Visible == false)
                    continue;
                dWidth += this[xx].Width;
            }

            return dWidth;
        }

        public int Width
        {
            get
            {
                return mWidth;
            }
        }

        [SupportedOSPlatform("windows")]
        public ZykHFGridColumn Add(string theTitle, int theWidth = 100, StringAlignment theVAlign = StringAlignment.Near)
        {
            var dItem = new ZykHFGridColumn()
            {
                Width = theWidth,
                Title = theTitle,
                MinimumSize = new Size(4, 0)
            };
            dItem.SetIndex(mItems.Count);
            dItem.SetColumnLeft(GetWidthTotal());
            switch (theVAlign)
            {
                case StringAlignment.Center:
                    {
                        dItem.TextAlign = ContentAlignment.MiddleCenter;
                        dItem.ItemAlign = ContentAlignment.MiddleCenter;
                        break;
                    }

                case StringAlignment.Far:
                    {
                        dItem.TextAlign = ContentAlignment.MiddleRight;
                        dItem.ItemAlign = ContentAlignment.MiddleRight;
                        break;
                    }

                case StringAlignment.Near:
                    {
                        dItem.TextAlign = ContentAlignment.MiddleLeft;
                        dItem.ItemAlign = ContentAlignment.MiddleLeft;
                        break;
                    }
            }

            mItems.Add(dItem);
            mHFGrid.Body.Clear();
            mWidth = GetWidthTotal();
            dItem.WidthChanged += WidthChanged;
            ColumnCountChanged?.Invoke();
            return dItem;
        }

        /// <summary>
        /// 列数量改变时发生
        /// </summary>
        /// <remarks></remarks>
        public event ColumnCountChangedEventHandler? ColumnCountChanged;

        public delegate void ColumnCountChangedEventHandler();

        public void Clear()
        {
            mItems.Clear();
            mWidth = 0;
            mHFGrid.Body.Clear();
            ColumnCountChanged?.Invoke();
        }

        public ZykHFGridColumn this[int index]
        {
            get
            {
                return mItems[index];
            }

            set
            {
                mItems[index] = value;
                value.SetIndex(index);
                mHFGrid.Body.Clear();
            }
        }

        private void WidthChanged()
        {
            int dLeft = 0;
            for (int xx = 0, loopTo = Count - 1; xx <= loopTo; xx++)
            {
                if (this[xx].Visible == true)
                {
                    this[xx].SetColumnLeft(dLeft);
                    dLeft += this[xx].Width;
                }
            }

            mWidth = dLeft;
            mHFGrid.Body.WidthChange();
            mHFGrid.Body.Refresh();
        }

        public void Refresh()
        {
            ColumnCountChanged?.Invoke();
            WidthChanged();
        }
    }
}